package com.ruoyi.web.controller.vip;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.vip.domain.Member;
import com.ruoyi.vip.service.impl.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vip/member")

public class MemberController extends BaseController {
    @Autowired
    private IMemberService memberService;

    /**
     * 查项列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Member member) {
        startPage();
        List< Member> list =  memberService.getList(member);
        TableDataInfo tableDataInfo = getDataTable(list);
        return tableDataInfo;
    }
    /**
     * 删除
     */
    @Log(title = "患者管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemsIds}")
    public AjaxResult remove(@PathVariable Integer[] itemsIds) {
        return toAjax(memberService.deleteMemberByIds(itemsIds));
    }

    /**
     * 新增
     */
    @Log(title = "患者管理", businessType = BusinessType.INSERT)
    @PostMapping("/addItem")
    public AjaxResult add(@RequestBody Member member) {

        String regTime = DateUtils.dateTime(member.getRegTimeForm());
        member.setRegTime(regTime);

        String birthday = DateUtils.dateTime(member.getBirthdayForm());
        member.setBirthday(birthday);

        if (memberService.getInfoById(member.getId()) != null) {
            return AjaxResult.error("该编号已存在");
        }
        if (memberService.getInfoByName(member.getName()) != null) {
            return AjaxResult.error("该名称已存在");
        }
        if (memberService.getByPhoneNumber(member.getPhoneNumber()) != null) {
            return AjaxResult.error("该号码已存在");
        }


        return toAjax(memberService.insertMember(member));
    }


    /**
     * 根据检查项id获取详情
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable Integer id) {
        return AjaxResult.success(memberService.getInfo(id));
    }


    //改

    @Log(title = "患者管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateMember")
    public AjaxResult updateMember(@RequestBody Member member) {

        String regTime = DateUtils.dateTime(member.getRegTimeForm());
        member.setRegTime(regTime);
        String birthday = DateUtils.dateTime(member.getBirthdayForm());
        member.setBirthday(birthday);

        Member memberIsCode = memberService.getInfoById(member.getId());
        if (memberIsCode != null) {
            if (!member.getId().equals(memberIsCode.getId())) {
                return AjaxResult.error("该编号已存在");
            }
        }

        Member memberIsName = memberService.getInfoByName(member.getName());
        if (memberIsName != null) {
            if (!member.getId().equals(memberIsName.getId())) {
                return AjaxResult.error("该名称已存在");
            }
        }
        Member memberIsPhoneNumber = memberService.getByPhoneNumber(member.getPhoneNumber());
        if (memberIsPhoneNumber != null) {
            if (!member.getId().equals(memberIsPhoneNumber.getPhoneNumber())) {
                return AjaxResult.error("该号码已存在");
            }
        }




        return toAjax(memberService.updateMember(member));
    }





}
